package com.ngovanhau.justsort.sound;

import com.ngovanhau.justsort.R;
import com.ngovanhau.justsort.control.ValueControl;

import android.content.Context;
import android.media.MediaPlayer;

public class MusicBackground {
	MediaPlayer mediaPlayer;
	
	public void loadMusic(Context context){
		mediaPlayer = MediaPlayer.create(context, R.raw.bg_music);
		mediaPlayer.setVolume(0.4f, 0.4f);
		mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
			
			@Override
			public void onCompletion(MediaPlayer mp) {
				// TODO Auto-generated method stub
				play();
			}
		});
	}
	
	public void play(){
		if(ValueControl.isMusic){
			mediaPlayer.seekTo(0);
			mediaPlayer.start();
		}
	}
	
	public void pause(){
		if(mediaPlayer.isPlaying())
			mediaPlayer.pause();
	}
	
	public void resume(){
		if(!mediaPlayer.isPlaying() && ValueControl.isMusic){
			mediaPlayer.start();	
			mediaPlayer.seekTo(0);
		}
	}
	
	public void release(){
		mediaPlayer.release();
	}
}
